package com.lenovo.lcdm.team.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lenovo.lcdm.team.common.util.IpUtil;
import com.lenovo.lcdm.team.entity.LoginLog;
import com.lenovo.lcdm.team.mapper.LoginLogMapper;
import com.lenovo.lcdm.team.service.ITeamUserService;
import com.lenovo.lcdm.team.service.LoginLogService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
* @author xujing30
* @description 针对表【login_log】的数据库操作Service实现
* @createDate 2024-03-21 10:52:24
*/
@Service
public class LoginLogServiceImpl extends ServiceImpl<LoginLogMapper, LoginLog>
    implements LoginLogService{

    @Autowired
    private ITeamUserService teamUserService;

    @Override
    public void loginSuccessLog(HttpServletRequest request, String itCode) {
        LoginLog log = new LoginLog();
        log.setItCode(itCode);
        log.setIpAddress(IpUtil.getIpAddr(request));
        log.setLoginTime(new Date());
        log.setLoginResult("Successful");
        this.save(log);
        //更新最后登陆时间
        teamUserService.updateLastLoginTime(itCode);
    }

    @Override
    public void loginFailedLog(HttpServletRequest request, String itCode, String errorMsg) {
        LoginLog log = new LoginLog();
        log.setItCode(itCode);
        log.setIpAddress(IpUtil.getIpAddr(request));
        log.setLoginTime(new Date());
        log.setLoginResult("Failed");
        log.setMessage(errorMsg==null?"":errorMsg);
        this.save(log);
    }
}




